package com.cloudsigns.factorymanager2.engineering.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.StringSignature;
import com.cloudsigns.factorymanager2.R;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by tony on 2018/5/17.
 */

public class GridAdapter extends BaseAdapter {

    private List<String> urlList = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;

    public GridAdapter(Activity activity) {
        context = activity;
        this.urlList = urlList;
//        if (urlList.size() == 4) {
//            urlList.remove(urlList.size() - 1);
//        }
        inflater = LayoutInflater.from(activity);
    }

    public void setData(List<String> tmpUrlList) {
        this.urlList.clear();
        if (null != tmpUrlList) {
            this.urlList.addAll(tmpUrlList);
        }
    }

    public int getCount() {
        return urlList.size();
    }

    @Override
    public String getItem(int position) {
        return urlList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_image, parent, false);
            holder.img_default = (ImageView) convertView.findViewById(R.id.img_default);
            holder.img_photo = (ImageView) convertView.findViewById(R.id.img_photo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String path = urlList.get(position);
        if (null != path && !TextUtils.isEmpty(path)) {
            if (path.equals("paizhao")) {
                holder.img_default.setVisibility(View.VISIBLE);
                holder.img_photo.setVisibility(View.GONE);
            } else {
                holder.img_default.setVisibility(View.GONE);
                holder.img_photo.setVisibility(View.VISIBLE);

//                holder.image.setImageResource(R.mipmap.photo);
//                Uri uri = Uri.parse("http:\/\/120.78.214.229:10000\/2018\/06\/02\/41f9612c-151a-484b-a768-0622afb6d738.png");
                path = path.replace("[", "");
                path = path.replace("]", "");

                Glide.with(context)
                        .load(path)
//                        .signature(new StringSignature(UUID.randomUUID().toString()))
                        .placeholder(R.mipmap.icon_no_photo)
                        .error(R.mipmap.icon_no_photo)
                        .centerCrop()
                        .crossFade()
                        .into(holder.img_photo);
            }
        }

        return convertView;
    }

    class ViewHolder {
        ImageView img_default;
        ImageView img_photo;

    }
}
